package com.jinbiao.quasar.controller;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.jinbiao.quasar.annotate.OperationLog;
import com.jinbiao.quasar.model.SysNotice;
import com.jinbiao.quasar.model.SysTodo;
import com.jinbiao.quasar.util.AjaxJson;
import org.noear.solon.annotation.Controller;
import org.noear.solon.annotation.Mapping;
import org.noear.solon.data.annotation.Tran;
import org.noear.solon.extend.sqltoy.annotation.Db;
import org.sagacity.sqltoy.dao.SqlToyLazyDao;
import org.sagacity.sqltoy.model.EntityUpdate;
import org.sagacity.sqltoy.model.Page;

import java.util.List;

@Controller
@OperationLog
public class TodoController {
    @Db
    SqlToyLazyDao dao;
    @Mapping("/todo/get-todo-list")
    public AjaxJson getTodoList(int page, int pageSize, SysTodo todo){
        todo.setCreatedBy(StpUtil.getLoginIdAsString());
        return AjaxJson.getSuccessData(dao.findPageBySql(new Page(pageSize,page),"sysTodoList",todo));
    }

    @Mapping("/todo/add-todo")
    @Tran
    public AjaxJson addTodo(SysTodo todo){
        dao.save(todo);
        return AjaxJson.getSuccess();
    }
    @Mapping("todo/edit-todo")
    @Tran
    public AjaxJson editTodo(SysTodo todo){
        dao.updateByQuery(SysTodo.class, EntityUpdate.create().set("todoDetail",todo.getTodoDetail()).set("todoStatus",todo.getTodoStatus()).where("id=?").values(todo.getId()));
        return AjaxJson.getSuccess();
    }
    @Mapping("/todo/query-todo-by-id")
    public AjaxJson queryTodoById(SysTodo todo){
        List<SysTodo> results = dao.findBySql("sysTodoList",todo);
        if(CollectionUtil.isNotEmpty(results)){
            return AjaxJson.getSuccessData(results.get(0));
        }
        return AjaxJson.getSuccess();
    }
}
